﻿using System.Web.Mvc;
using Complaint.Models;
using Complaint.Models.Dtos;

namespace Complaint.Areas.Admin.Controllers
{
    public class SettlementTypeController : Controller
    {
        //
        // GET: /Admin/SettlementType/

        public ActionResult Index()
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new SettlementTypeModel();
                return View(model.Retrieve());
            }
        }

        //
        // GET: /Admin/SettlementType/Add

        public ActionResult Add()
        {
            var settlementType = new SettlementType();
            return PartialView("SettlementTypeForm", settlementType);
        } 

        //
        // POST: /Admin/SettlementType/Create

        [HttpPost]
        public ActionResult Add(SettlementType settlementType)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    using (new UnitOfWorkScope(true))
                    {
                        var model = new SettlementTypeModel();
                        model.Create(settlementType);

                        return PartialView("ListControl", model.Retrieve());
                    }
                }
                catch
                {
                    return Content("Failed");
                }
            }

            return PartialView("ListControl", new SettlementType());
        }
        
        //
        // GET: /Admin/SettlementType/Edit/5
 
        public ActionResult Edit(int id)
        {
            using (new UnitOfWorkScope(true))
            {
                var model = new SettlementTypeModel();
                var settlementType = model.FindById(id);
                return PartialView("SettlementTypeForm", settlementType);
            }
        }

        //
        // POST: /Admin/SettlementType/Edit/5

        [HttpPost]
        public ActionResult Edit(SettlementType settlementType)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new SettlementTypeModel();
                    var t = model.FindById(settlementType.SettlementTypeID);
                    t.Type = settlementType.Type;
                    t.Description = settlementType.Description;
                    model.Update(t);

                    return PartialView("ListControl", model.Retrieve());
                }
            }
            catch
            {
                return Content("Failed");
            }
        }

        //
        // GET: /Admin/SettlementType/Delete/5
 
        public ActionResult Delete(int id)
        {
            try
            {
                using (new UnitOfWorkScope(true))
                {
                    var model = new SettlementTypeModel();

                    model.Delete(model.FindById(id));
                    return PartialView("ListControl", model.Retrieve());
                }
            }
            catch
            {
                // error handling here
                return Content("Failed to delete due to an error.");
            }
        }

    }
}
